﻿@{
    Layout = null;
}

<link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form" lay-filter="editForm" action="">
    <div class="layui-form-item">
        <input type="hidden" name="id" />
        <label class="layui-form-label">客户编码</label>
        <div class="layui-input-block">
            <input type="text"
                   name="customerCode"
                   autocomplete="off"
                   placeholder="请输入"
                   lay-verify="required"
                   class="layui-input"
                   style="width: 200px" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">客户名称</label>
        <div class="layui-input-block">
            <input type="text"
                   name="customerName"
                   autocomplete="off"
                   placeholder="请输入"
                   lay-verify="required"
                   class="layui-input"
                   style="width: 200px" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">客户简称</label>
        <div class="layui-input-block">
            <input type="text"
                   name="customerAbbreviation"
                   autocomplete="off"
                   placeholder="请输入"
                   lay-verify="required"
                   class="layui-input"
                   style="width: 200px" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">客户类型</label>
        <div class="layui-input-inline">
            <select name="customerType" lay-filter="aihao">
                <option value=""></option>
                <option value="1" selected>企业客户</option>
                <option value="2">个人客户</option>
                <option value="3">未知类型</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">客户电话</label>
        <div class="layui-input-block">
            <input type="text"
                   name="customerPhone"
                   autocomplete="off"
                   placeholder="请输入"
                   lay-verify="required"
                   class="layui-input"
                   style="width: 200px" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">联系人</label>
        <div class="layui-input-block">
            <input type="text"
                   name="contactName"
                   autocomplete="off"
                   placeholder="请输入"
                   lay-verify="required"
                   class="layui-input"
                   style="width: 200px" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">联系人电话</label>
        <div class="layui-input-block">
            <input type="text"
                   name="contactPhone"
                   autocomplete="off"
                   placeholder="请输入"
                   lay-verify="required"
                   class="layui-input"
                   style="width: 200px" />
        </div>
    </div>
    <div class="layui-form-item" pane>
        <label class="layui-form-label">是否有效</label>
        <div class="layui-input-block">
            <input type="radio" name="state" id="state" value="true" title="是" checked />
            <input type="radio" name="state" id="state" value="false" title="否" />
        </div>
    </div>
    <div class="layui-form-item">
        <button class="layui-btn" lay-submit lay-filter="demo2">确认</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</form>

<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/layui-v2.5.6/layui/layui.js"></script>

<script>
    layui.use(['form'], function(){
           var form = layui.form;
           var $ = layui.$;
           var layer = layui.layer;

           // 加载数据方法
           window.loadData = function(id) {
               $.get('/CustomerManagement/GetCustomerById', { id: id }, function(res){
                   console.log('获取到的数据:', res);
                   if(res.code === 0){
                       var formData = {
                           id: res.data.id,
                           customerCode: res.data.customerCode,
                           customerName: res.data.customerName,
                           customerAbbreviation: res.data.customerAbbreviation,
                           customerType: res.data.customerType,
                           customerPhone: res.data.customerPhone,
                           contactName: res.data.contactName,
                           contactPhone: res.data.contactPhone,
                                 state: res.data.state ? "true" : "false"
                       };
                       form.val('editForm', formData);
                       form.render();
                   } else {
                       layer.msg('获取数据失败');
                   }
               });
           }

           // 提交事件
            form.on('submit(demo2)', function (data) {
              var field = data.field; // 获取表单字段值
              // 显示填写结果，仅作演示用
              // layer.alert(JSON.stringify(field), {
              //   title: '当前填写的字段值',
              // });
              // 此处可执行 Ajax 等操作

                  $.ajax({
                              url:"/CustomerManagement/UpdateCustomer",
                              data:field,
                              type:"post",
                              dataType:"json",
                              success:function(res){
                                  console.log(res)
                                  if(res>0)
                                  {
                                     alert("修改成功")
                                     // 刷新父页面表格
                              parent.layui.table.reload('ID-table-demo-theads-2');
                              // 关闭弹窗
                              var index = parent.layer.getFrameIndex(window.name);
                              parent.layer.close(index);
                                  }
                                  else
                                  {
                                      alert("修改失败")
                                  }
                              }
                          })
              return false; // 阻止默认 form 跳转
            });
    });
</script>

